import random

import allure
from selenium.webdriver.common.by import By


from utils.wait_for_text import wait_for_text
from wewrok_app_po.Base.wework_app import WeWorkApp


class ManualInputPage(WeWorkApp):
    # def __init__(self, driver:WebDriver):
    #     self.driver = driver
    _SHURU = By.XPATH, "//*[@resource-id='com.tencent.wework:id/l6i']"
    _INPUT_NAME = By.XPATH, "//*[@text='姓名']/parent::*/child::*[3]"
    _INPUT_PHONE = By.XPATH, "//*[@text='手机号']"
    _INPUT_ID = By.XPATH, "//*[@text='帐号']/parent::*/child::*[3]"
    _INPUT_EMAIL = By.XPATH, "//*[@text='企业邮箱']/parent::*/child::*[2]"
    _SAVE = By.XPATH, "//*[@resource-id='com.tencent.wework:id/az1']"

    # 输入成员的信息
    # 点击保存按钮
    def input_member_info(self,name,phone):
        from wewrok_app_po.Page.add_member_page import AddMemberPage

        # 判断是否为完整输入
        # res = self.driver.find_element(By.XPATH, "//*[@resource-id='com.tencent.wework:id/l6i']")
        res = self.find(*self._SHURU)
        if res.text == '完整输入':
            res.click()

        # 输入姓名
        # self.driver.find_element(By.XPATH, "//*[@text='姓名']/parent::*/child::*[3]").send_keys(name)
        with allure.step("输入姓名"):

            self.find_and_send_keys(*self._INPUT_NAME,name)

        # 输入账号
        # self.driver.find_element(By.XPATH, "//*[@text='帐号']/parent::*/child::*[3]").send_keys(phone)
        with allure.step("输入账号"):
            self.find_and_send_keys(*self._INPUT_ID,phone)
        # 性别
        sex = ['男', '女']
        # 随机性别
        sex_result = random.choice(sex)
        # 点击性别
        self.driver.find_element(By.XPATH, "//*[@text='性别']/parent::*/child::*[2]").click()
        # 随机选择性别
        ctrl2 = wait_for_text(self.driver, sex_result)

        if ctrl2 == True:
                self.driver.find_element(By.XPATH, f'//*[@text="{sex_result}"]').click()

        # 输入企业邮箱
        # self.driver.find_element(By.XPATH, "//*[@text='企业邮箱']/parent::*/child::*[2]").send_keys(phone)
        with allure.step("输入企业邮箱"):
            self.find_and_send_keys(*self._INPUT_EMAIL,phone)
        # 输入手机号
        # self.driver.find_element(By.XPATH, "//*[@text='手机号']").send_keys(phone)
        with allure.step("输入手机号"):
            self.find_and_send_keys(*self._INPUT_PHONE,phone)
        self.driver.implicitly_wait(1)
        # 调用滑动方法,点击保存按钮
        # swipe_element(self.driver, By.XPATH, "//*[@resource-id='com.tencent.wework:id/az1']").click()
        with allure.step("点击保存按钮"):
            self.swipe_element(*self._SAVE).click()
        # 跳转到添加成员页面
        return AddMemberPage(self.driver)